tinyMCEPopup.requireLangPack();

var StyleLibraryExamplesDialog = {
	init : function() {
		var f = document.forms[0];

		//f.someval.value = tinyMCEPopup.editor.selection.getContent({format : 'text'});

		// Repeat example tables
		var html = document.getElementById("demotable").innerHTML;
		var parent = document.getElementById("tables");
		this.generateDemos(html, parent, tables);
		
		// Repeat example textboxes
		html = document.getElementById("demotextbox").innerHTML;
		parent = document.getElementById("textboxes");
		this.generateDemos(html, parent, textboxes);

		// Repeat example images
		html = document.getElementById("demoimage").innerHTML;
		parent = document.getElementById("images");
		this.generateDemos(html, parent, images);
		
		// Remove any extra style sheets for 100% preview
		var headNodes = document.getElementsByTagName("head");
		var styles = headNodes[0].getElementsByTagName("link");
		for(var i = 0; i < styles.length; i++)
		{
			if(styles[i].title != "wysiwyg") {
				styles[i].disabled = true;
			}
		}	

	},

	// Generate HTML for each demo class
	generateDemos : function(html, parent, classes)
	{
		var left = true;
		var titleRx = new RegExp("{title}", "g");
		
		for (var i = 0; i < classes.length; i++) {
			
			var output = html.replace(/replaceme/g, classes[i] + (left ? " left" : " right"));
			output = output.replace(titleRx, classes[i]);
			
			var demoElm = document.createElement("div");
			demoElm.innerHTML = output;
			demoElm.className = "demo";
			parent.appendChild(demoElm);
			left = !left;
		}
	},
	
	// Jump to style designer pages
	copyStyle : function(selectedPanel, styleName)
	{
		document.location.href = this.getBaseUrl(selectedPanel) + "&class=" + styleName + "&examples=1";
	},
	
	noCopyStyle : function(selectedPanel)
	{
		document.location.href = this.getBaseUrl(selectedPanel);
	},
	
	getBaseUrl : function(selectedPanel)
	{
		switch(selectedPanel)
		{
			case "tables":
				return "TableEdit.aspx?styles=tables";
				break;
			case "images":
				return "ImageEdit.aspx?styles=images";
				break;
			case "textboxes":
				return "TextBoxEdit.aspx?styles=textboxes";
				break;
		}
	}
};

tinyMCEPopup.onInit.add(StyleLibraryExamplesDialog.init, StyleLibraryExamplesDialog);
